package com.JavaNet.chapter8_client_socket.redisClient;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

/**
 * @author : wushikai
 * <p>
 * date : 2021-04-20
 * <p>
 * jboot.cache.type=redis
 * jboot.cache.redis.host=172.16.40.226
 * jboot.cache.redis.password=1BD17118-19EA-4543-871B-65683647AD9C
 * jboot.cache.redis.database=1
 */


public class RedisDemoClient {

    private Socket redisConnection = null;

    private RedisDemoClient(String host, int port) throws IOException {
        redisConnection = new Socket(host, port);
    }



    public void set(){

        try {
            //1.建立客户端socket连接，指定服务器位置及端口
            Socket socket =new Socket("localhost",6379);
            //2.得到socket读写流
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            //输入流
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            //3.利用流按照一定的操作，对socket进行读写操作
            //String info="*3\r\n$3\r\nSET\r\n$3\r\n"+"zk"+"\r\n$1\r\n"+"zookeeper"+"\r\n";
            //这个不能随意, 这字符串包含了 redis  的 协议;
            String info="*3\r\n$3\r\nSET\r\n$3\r\nzmk\r\n$1\r\nz\r\n";
            pw.write(info);
            pw.flush();
            socket.shutdownOutput();
            //接收服务器的相应
            String reply;
            while(!((reply= br.readLine()) == null)){
                System.out.println(reply);
            }
            //4.关闭资源
            br.close();
            is.close();
            pw.close();
            os.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public void set(String key, String value) throws IOException {

        StringBuilder request = new StringBuilder();

       request.append("*3").append("\r\n");

        request.append("$3").append("\r\n"); //参数的长度
        request.append("SET").append("\r\n"); // 参数的值

        request.append("$").append(key.getBytes().length).append("\r\n");
        request.append(key).append("\r\n");

        request.append("$").append(value.getBytes().length).append("\r\n");
        request.append(value).append("\r\n");




        System.out.println("这就是客户端发给redis服务器数据:");
        System.out.println("request ==> " + request);

        // 发送数据到redis服务器
        redisConnection.getOutputStream().write(request.toString().getBytes());

        //如何接受redis服务器的相应
        byte[] buffer = new byte[1024];
        InputStream inputStream = redisConnection.getInputStream();
       // InputStreamReader reader  = new InputStreamReader(inputStream);
        int len = buffer.length;
        while (  ( len = inputStream.read( buffer , 0, len))>0 ){

            String str = new String( buffer, StandardCharsets.UTF_8);
            System.out.print(str);
        }
        System.out.println();

        inputStream.close();

        redisConnection.close();

    }


    private String get(String key) throws IOException {

        StringBuilder request = new StringBuilder();
        request.append("*2").append("\r\n");

        request.append("GET").append("\r\n");

        request.append("$").append(key.getBytes().length).append("\r\n");
        request.append(key).append("\r\n");

        System.out.println("这就是客户端发送给Redis服务器的数据");
        System.out.println(request);

        redisConnection.getOutputStream().write(request.toString().getBytes());

        // 如何接受redis 服务器的响应
        byte[] response = new byte[1024];
        redisConnection.getInputStream().read(response);

        return new String(response);

    }


    public static void main(String[] args) throws IOException {
        RedisDemoClient redisDemoClient = new RedisDemoClient("127.0.0.1", 6379);
        redisDemoClient.set( );

        //redisDemoClient.get("testKey");
    }


}
